 

//
//  ZMNetworking.h
//  dudu_oc_master
//
//  Created by mac on 2019/11/19.
//  Copyright © 2019 Shawn Ma. All rights reserved.
//


#import "TJNavigationBar.h"

#define MARGIN 5
#define DEFAULT_LEFTMAGRIN 8
#define DEFAULT_RIGHTMAGRIN 12
#define DEFAUL_BUTTONW 40
#define DEFAUL_STARCKH 40
#define DEFAUL_ALPHA 0.2


@interface TJBackButton : TJButtonBarButton

@end

@implementation TJBackButton

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect imageViewFrame = self.imageView.frame;
    imageViewFrame.origin.x = 5;
    self.imageView.frame = imageViewFrame;
    
    CGRect titleLabelFrame = self.titleLabel.frame;
    titleLabelFrame.origin.x = CGRectGetMaxX(self.imageView.frame) + 3;
    self.titleLabel.frame = titleLabelFrame;
}

@end

@interface TJButtonBarButton()

@property (strong, nonatomic) UIColor *textColor;
@property (strong, nonatomic) UIFont *textFont;


@end


@implementation TJButtonBarButton

- (void)setHighlighted:(BOOL)highlighted {
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _textFont = [UIFont systemFontOfSize:15];
        _textColor = [UIColor blackColor];
    }
    return self;
}

+ (instancetype)barButtonWithTarget:(nullable id)target action:(nullable SEL)action {
    TJButtonBarButton *barButton = [[self alloc] init];
    [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return barButton;
}

+ (instancetype)barButtonWithImageName:(nullable NSString *)imageName target:(nullable id)target action:(nullable SEL)action {
    TJButtonBarButton *barButton = [[self alloc] init];
    UIImage *normalImage = [UIImage imageNamed:imageName];
    [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [barButton setImage:normalImage forState:UIControlStateNormal];
    [barButton setImage:normalImage forState:UIControlStateHighlighted];
    return barButton;
}

+ (instancetype)barButtonWithTitle:(nullable NSString *)title target:(nullable id)target action:(nullable SEL)action {
    TJButtonBarButton *barButton = [[self alloc] init];
    [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [barButton setTitleColor:barButton.textColor forState:UIControlStateNormal];
    [barButton setTitleColor:barButton.textColor forState:UIControlStateHighlighted];
    barButton.titleLabel.font = barButton.textFont;
    [barButton setTitle:title forState:UIControlStateNormal];
    return barButton;
}

+ (instancetype)barButtonWithTitle:(nullable NSString *)title color:(UIColor *)color target:(nullable id)target action:(nullable SEL)action {
    TJButtonBarButton *barButton = [[self alloc] init];
    [barButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [barButton setTitleColor:color forState:UIControlStateNormal];
    [barButton setTitleColor:color forState:UIControlStateHighlighted];
    [barButton setTitle:title forState:UIControlStateNormal];
    barButton.titleLabel.font = barButton.textFont;
    return barButton;
    
}

+ (instancetype)barButtonBackButtonTarget:(nullable id)target action:(nullable SEL)action {
    return [TJBackButton barButtonWithImageName:@"nav_back_default" target:target action:action];
}

+ (instancetype)barButtonBackButtonImageName:(NSString *)imageName target:(nullable id)target action:(nullable SEL)action {
    return [TJBackButton barButtonWithImageName:imageName target:target action:action];
}

@end


@interface TJButtonBarStackView : UIView

@property (assign, nonatomic) CGFloat spacing;

@end


@implementation TJButtonBarStackView

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            obj.frame = CGRectMake(0, (self.bounds.size.height - obj.bounds.size.height) * 0.5, obj.bounds.size.width, obj.bounds.size.height);
        } else {
            UIView *previousView = self.subviews[idx - 1];
            TJLog(@"_spacing = %f", _spacing);
            obj.frame = CGRectMake(CGRectGetMaxX(previousView.frame) + self.spacing, (self.bounds.size.height - obj.bounds.size.height) * 0.5, obj.bounds.size.width, obj.bounds.size.height);
        }
    }];
}

- (void)setSpacing:(CGFloat)spacing {
    _spacing = spacing;
    
    [self layoutIfNeeded];
}

@end

@interface TJNavigationBar()

@property (strong, nonatomic) UIView *navigationBarContentView;
@property (strong, nonatomic) UIImageView *backgroundView;
@property (strong, nonatomic) TJButtonBarStackView *leftBarButtonsStackView;
@property (strong, nonatomic) TJButtonBarStackView *rightBarButtonsStackView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (assign, nonatomic) CGFloat statusBarHeight;
 
@end


@implementation TJNavigationBar

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
                
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, KStatusBarHight + 44);
        [self addSubview:self.backgroundView];
        [self addSubview:self.navigationBarContentView];
        [self.navigationBarContentView addSubview:self.lineView];
        
    }
    return self;
}

- (UIView *)navigationBarContentView {
    if (!_navigationBarContentView) {
        _navigationBarContentView = [[UIView alloc] initWithFrame:CGRectMake(0, KStatusBarHight,  [UIScreen mainScreen].bounds.size.width, self.frame.size.height - KStatusBarHight)];
    }
    return _navigationBarContentView;
}

- (UIImageView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
        _backgroundView.userInteractionEnabled = YES;
        _backgroundView.backgroundColor = [UIColor whiteColor];
    }
    return _backgroundView;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.navigationBarContentView.frame) - 0.5, KScreenWidth, 0.5)];
        _lineView.backgroundColor = UIColor.tj_separatorColor;
    }
    return _lineView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = MediumFont(17);
        [self.navigationBarContentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
            make.width.mas_lessThanOrEqualTo(KScreenWidth - 150);
        }];
    }
    return _titleLabel;
}

- (void)setTitleView:(UIView *)titleView {
    _titleView = titleView;
    
    CGSize size = titleView.frame.size;
    
    if (!size.width || !size.height) return;
    
    [_titleLabel removeFromSuperview];
    _titleLabel = nil;
    
    [self.navigationBarContentView addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.size.mas_equalTo(size);
        make.width.mas_lessThanOrEqualTo(KScreenWidth - 150);
    }];
}

- (void)setLeftBarButton:(TJButtonBarButton *)leftBarButton {
    _leftBarButton = leftBarButton;
    
    if (!leftBarButton) return;
    
    self.leftBarButtons = @[leftBarButton];
}


- (TJButtonBarButton *)backBarButton {
    __block TJButtonBarButton *backBarButton = nil;
    [self.leftBarButtons enumerateObjectsUsingBlock:^(TJButtonBarButton *barButton, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([barButton isKindOfClass:TJBackButton.class]) {
            backBarButton = barButton;
            *stop = YES;
        }
    }];
    return backBarButton;
}

- (void)setLeftBarButtons:(NSArray<TJButtonBarButton *> *)leftBarButtons {
    _leftBarButtons = leftBarButtons;
    
    [self removeStackViewAllSubView:self.leftBarButtonsStackView];
    
    [leftBarButtons enumerateObjectsUsingBlock:^(TJButtonBarButton *barButton, NSUInteger idx, BOOL * _Nonnull stop) {
        [self resizeBarButtonFrame:barButton];
        
        [self.leftBarButtonsStackView addSubview:barButton];
        
    }];
    
    UIView *lastView = self.leftBarButtonsStackView.subviews.lastObject;
    if (!lastView) return;
    
    self.leftBarButtonsStackView.spacing = 4;
    [self.leftBarButtonsStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(DEFAULT_LEFTMAGRIN);
        make.top.offset(0);
        make.bottom.offset(-1);
        make.right.equalTo(lastView);
    }];
    
    if (_titleLabel) self.title = self.titleLabel.text;
}


- (void)setRightBarButton:(TJButtonBarButton *)rightBarButton {
    _rightBarButton = rightBarButton;
    
    if (!rightBarButton) {
        [self removeStackViewAllSubView:self.rightBarButtonsStackView];
        return;
    };
    self.rightBarButtons = @[rightBarButton];
}

- (void)setRightBarButtons:(NSArray<TJButtonBarButton *> *)rightBarButtons {//只管布局
    _rightBarButtons = rightBarButtons;
    
    [self removeStackViewAllSubView:self.rightBarButtonsStackView];
    
    [rightBarButtons enumerateObjectsUsingBlock:^(TJButtonBarButton *barButton, NSUInteger idx, BOOL * _Nonnull stop) {
        [self resizeBarButtonFrame:barButton];
        [self.rightBarButtonsStackView addSubview:barButton];
    }];
    
    UIView *lastView = self.rightBarButtonsStackView.subviews.lastObject;
    if (!lastView) return;
    
    self.rightBarButtonsStackView.spacing = 2;
    [self.rightBarButtonsStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-DEFAULT_RIGHTMAGRIN);
        make.top.offset(0);
        make.bottom.offset(-1);
        make.right.equalTo(lastView);
    }];
    
    if (_titleLabel) self.title = self.titleLabel.text;
}

- (void)resizeBarButtonFrame:(TJButtonBarButton *)barButton {
    
    CGSize tj_size =  CGSizeZero;
    if (barButton.tj_size.width && barButton.tj_size.height) {
        
        if (barButton.backgroundColor) {
            tj_size = CGSizeMake(barButton.tj_size.width, MIN(barButton.tj_size.height, DEFAUL_STARCKH));
        } else {
            tj_size = CGSizeMake(barButton.tj_size.width, DEFAUL_STARCKH);
        }
    } else {
        [barButton sizeToFit];
        tj_size = CGSizeMake(MAX(barButton.tj_width, DEFAUL_BUTTONW), DEFAUL_STARCKH);
    }
    
    barButton.tj_size = tj_size;
    
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    self.titleLabel.text = title;
    
    if (_titleLabel.superview) {
        return;
    }
    
    if (_leftBarButtonsStackView) {
        [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.greaterThanOrEqualTo(self.leftBarButtonsStackView.mas_right).offset(DEFAULT_LEFTMAGRIN);
        }];
    }
    
    if (_rightBarButtonsStackView) {
        [_titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.lessThanOrEqualTo(self.rightBarButtonsStackView.mas_left).offset(-DEFAULT_RIGHTMAGRIN);
        }];
    }
    
}
- (void)setTitleTextAttributes:(NSDictionary *)titleTextAttributes {
    _titleTextAttributes = titleTextAttributes;
    
    self.titleLabel.attributedText = [[NSAttributedString alloc] initWithString:_titleLabel.text attributes:titleTextAttributes];
}

- (void)setTitleTextColor:(UIColor *)titleTextColor {
    _titleTextColor = titleTextColor;
    self.titleLabel.textColor = titleTextColor;
}

- (void)setRightBarView:(UIView *)rightBarView {
    _rightBarView = rightBarView;
    
    
    [self removeStackViewAllSubView:self.rightBarButtonsStackView];
    
    [self.rightBarButtonsStackView addSubview:rightBarView];
    CGSize size = rightBarView.bounds.size;
    rightBarView.tj_size = CGSizeMake(size.width, DEFAUL_STARCKH);
    
    [self.rightBarButtonsStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-DEFAULT_RIGHTMAGRIN);
        make.centerY.offset(0);
        make.size.equalTo(rightBarView);
    }];
    
    if (_titleLabel) self.title = self.titleLabel.text;
}


- (void)setBackgroundColor:(UIColor *)backgroundColor {
    //没有重新父类的
    self.backgroundView.image = nil;
    self.backgroundView.backgroundColor = backgroundColor;
}

- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImage = backgroundImage;
    self.backgroundView.backgroundColor = nil;
    self.backgroundView.image = backgroundImage;
}

- (void)setAlpha:(CGFloat)alpha {
    self.backgroundView.alpha = alpha;
    self.lineView.hidden = !(alpha >= 1);
}

- (void)setHiddenBackButton:(BOOL)hiddenBackButton {
    _hiddenBackButton = hiddenBackButton;
    
    if (hiddenBackButton) [self.leftBarButtonsStackView removeFromSuperview];
}

- (void)setBarTintColorColor:(UIColor *)barTintColorColor {
    _barTintColorColor = barTintColorColor;
    
    self.backgroundView.image = nil;
    self.backgroundView.backgroundColor = barTintColorColor;
    
}


- (void)removeStackViewAllSubView:(TJButtonBarStackView *)tackView {
    [tackView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
        obj = nil;
    }];
}

- (TJButtonBarStackView *)leftBarButtonsStackView {
    if (!_leftBarButtonsStackView) {
        _leftBarButtonsStackView = [[TJButtonBarStackView alloc] init];
        _rightBarButtonsStackView.spacing = 4;
        [self.navigationBarContentView addSubview:_leftBarButtonsStackView];
    }
    return _leftBarButtonsStackView;
}

- (TJButtonBarStackView *)rightBarButtonsStackView {
    if (!_rightBarButtonsStackView) {
        _rightBarButtonsStackView = [[TJButtonBarStackView alloc] init];
        _rightBarButtonsStackView.spacing = 2;
        [self.navigationBarContentView addSubview:_rightBarButtonsStackView];
    }
    return _rightBarButtonsStackView;
}

- (void)addSubviewToContentView:(UIView *)view {
    [self.navigationBarContentView addSubview:view];
}

- (void)backBarButtonWhiteColor {
   [self.backBarButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
}

- (void)backBarButtonBlackColor {
    [self.backBarButton setImage:[UIImage imageNamed:@"nav_back_default"] forState:UIControlStateNormal];
}

@end
